import logging
import time

import pytest

from resource.pom.hap import Hap


@pytest.fixture(autouse=True)
def install_hap(device):
    device.install_hap(Hap.package_path)
    yield


class Test:
    hap = Hap

    @pytest.mark.parametrize('setup_teardown', [None], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1：长按已安装的HAP包')
        device.refresh_layout()
        show_name = device.get_element_by_text(self.hap.name)
        x, y = device.center_of_element(show_name)
        device.long_click(x, y - 50)
        device.refresh_layout()
        logging.info('预期结果1：长按HAP包应用，显示卸载按钮')
        device.assert_text_exist('卸载')

        logging.info('步骤2：点击卸载')
        uninstall_menu = device.get_element_by_text('卸载')
        device.click_element(uninstall_menu)
        logging.info('预期结果2：点击卸载，也可取消卸载，且不影响其他应用使用')
        device.refresh_layout()
        cancel = device.get_element_by_text('取消')
        uninstall_btn = device.get_element_by_text('卸载')
        device.click_element(cancel)
        device.refresh_layout()
        device.assert_text_exist(self.hap.name)
        device.long_click(x, y - 50)
        device.click_element(uninstall_menu)
        device.click_element(uninstall_btn)
        device.refresh_layout()
        device.assert_text_exist(self.hap.name, expect=False)
        assert self.hap.bundle_name not in device.hdc_shell('bm dump -a')
